package com.lishe.lishe.lishe.module.mall.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.activity.LoadMoreActivity;
import com.lishe.lishe.lishe.module.mall.activity.GoodsDetailActivity;
import com.lishe.lishe.lishe.module.mall.bean.MallGoods;

import java.util.List;

/**
 * Created by shino on 2017/11/6.
 * 商城首页请选活动滚动item
 */

public class MallSelectActivityItemAdapter extends RecyclerviewBaseAdapter {
    List<MallGoods> mallGoods;
    Activity context;

    public MallSelectActivityItemAdapter(Activity context, List<MallGoods> mallGoods) {
        this.mallGoods = mallGoods;
        this.context = context;
    }

    public void setData(List<MallGoods> mallGoods) {
        this.mallGoods = mallGoods;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mall_scroll0, null);
        ItemViewHolder holder = new ItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder0, final int position) {
        ItemViewHolder holder = (ItemViewHolder) holder0;
        final MallGoods mallItemObject = mallGoods.get(position);
        //if(!mallItemObject.getTitle().equals("more") && Integer.parseInt(mallItemObject.getPrice()) != 0){
        if(position == mallGoods.size() - 1){
            holder.simpleLayout.setVisibility(View.GONE);
            holder.moreLayout.setVisibility(View.VISIBLE);
        }else{
            holder.simpleLayout.setVisibility(View.VISIBLE);
            holder.moreLayout.setVisibility(View.GONE);
        }
        holder.goods_name.setText(mallItemObject.getTitle());
        holder.integral.setText("¥" + mallItemObject.getPrice());
        ImageLoaderUtils.displayImage(context, mallItemObject.getImage_default_id(), holder.goods_img);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == mallGoods.size() - 1) {
                    //查看更多
                    LoadMoreActivity.jump((Activity) context, mallItemObject.getItem_id(), mallItemObject.getTitle());
                } else {
                    GoodsDetailActivity.jump((Activity) context, mallItemObject.getItem_id());
                }
                //MallMainAdapterPresenter.activityItemOnClick((Activity) context, mallItemObject);
            }
        });

        if (position == 0) {
            holder.leftArea.setVisibility(View.VISIBLE);
            holder.rightArea.setVisibility(View.GONE);
        } else if (position == mallGoods.size() - 1) {
            holder.leftArea.setVisibility(View.GONE);
            holder.rightArea.setVisibility(View.VISIBLE);
        } else {
            holder.leftArea.setVisibility(View.GONE);
            holder.rightArea.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCountMy() {
        return mallGoods == null ? 0 : mallGoods.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        //item
        private ImageView goods_img;
        private TextView goods_name;
        private TextView integral;
        private LinearLayout integralLayout;
        private View leftArea, rightArea;
        private View simpleLayout, moreLayout;

        public ItemViewHolder(View v) {
            super(v);
            goods_img = v.findViewById(R.id.goods_img);
            goods_name = v.findViewById(R.id.goods_name);
            integral = v.findViewById(R.id.integral);
            integralLayout=v.findViewById(R.id.integral_layout);
            leftArea = v.findViewById(R.id.left_area);
            rightArea = v.findViewById(R.id.right_area);
            simpleLayout = v.findViewById(R.id.simple_layout);
            moreLayout = v.findViewById(R.id.more_layout);
        }
    }
}
